//
//  ViewControllerShop.m
//  测试
//
//  Created by 狮子座 on 2017/4/20.
//  Copyright © 2017年 森之源. All rights reserved.
//

#import "ViewControllerShop.h"
#import "accountsToolBar.h"
#import "shopViewModel.h"
#import "shopTableViewCell.h"
#import "UIViewController+Message.h"
#import "NSObject+YU.h"
@interface ViewControllerShop ()<accountsToolBarDelegate,MCTableViewCellDelegate,shopTableViewCellDelegate>
@property (weak,nonatomic)IBOutlet accountsToolBar *toolBar;
//@property (strong,nonatomic)NSMutableSet *selModels;
@property (strong,nonatomic)NSMutableArray *selModels;
@property (strong,nonatomic)id model;
@end

@implementation ViewControllerShop


-(id)onCreateViewModle
{
    return [shopViewModel new];
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    
//    [shopTableViewCell ];
}
-(NSMutableArray *)selModels
{
    if (!_selModels) {
        _selModels = [NSMutableArray array];
    }
    return _selModels;
}

//-(NSMutableSet *)selModels
//{
//    if (!_selModels) {
//        _selModels = [NSMutableSet set];
//    }
//    return _selModels;
//}


-(void)onUpdateCell:(UITableViewCell *)cell model:(id)model atIndexPath:(NSIndexPath *)indexPath
{
    [cell update:model atIndexPath:indexPath];
}

-(void)didSelectButtonWithModel:(shopModel *)model
{

//    NSLog(@"%@----%@",self.model,model);
    
//    if (![self.model isEqual:model]) {
//        NSLog(@"1111111--->%@",model);
//    }
    
    if (YES==model.isSel) {
//        NSLog(@"%d",[_selModels containsObject:model]);
    
//        if ([self.selModels containsObject:model]) {
//          [self.selModels removeObject:model];
//        }
        [self.selModels addObject:model];
    
  
//    self.model = model;
//
    }
        else
    {
        NSLog(@"%d",[_selModels containsObject:model]);
        if ([_selModels containsObject:model]) {
//            [_selModels removeAllObjects];
            [_selModels removeObject:model];
        }else
        {
            NSLog(@"%@",_selModels);
        }
    }
//
    NSString *price = [NSString stringWithFormat:@"%.2f",[self onMuchPrice]];
    NSLog(@"%@",price);
    [self.toolBar onUpdatePrice:price];
//    NSLog(@"%f",[self onMuchPrice]);
    
    
}

-(void)didSelectType
{

//    [model setMuch:m];
//    
//    [cell updateMuch];
    
    NSString *price = [NSString stringWithFormat:@"%.2f",[self onMuchPrice]];
//    NSLog(@"%@",price);
    [self.toolBar onUpdatePrice:price];
   
}


-(float )onMuchPrice
{
       float price = 0;
       for (shopModel *model in _selModels) {
           float h =model.price*model.much;
           NSLog(@"%f",h);
           price += h;
    }
    
    return price;
}



-(void)onSelectType:(selectToolType)type Button:(UIButton *)button
{
    //删除模块
    if (type==selectToolTypeDelete) {
        if (_selModels.count==0) return;
//        NSArray *sortSetArray = [self.selModels allObjects];
        [self.viewModel.modelsProxy removeObjectsInArray:_selModels];
        [self.tableView reloadData];
        [_selModels removeAllObjects];
        NSString *price = [NSString stringWithFormat:@"%.2f",[self onMuchPrice]];
        [self.toolBar onUpdatePrice:price];
    }
    //全选模块
    else if (type == selectToolTypeAll)
    {
        if (self.viewModel.modelsProxy.count==0){
            [self showBarMessage:@"亲,你没有商品!!"];
            [button setEnabled:NO];
            [button performBlock:^{
               [button setEnabled:YES];
            } afterDelay:1.25f];
            return ;
        }
        
        for (shopModel *model in self.viewModel.modelsProxy) {
            model.isSel = YES;
            [self.selModels addObject:model];
        }
        
        
        NSString *price = [NSString stringWithFormat:@"%.2f",[self onMuchPrice]];
        NSLog(@"%@",price);
        [self.toolBar onUpdatePrice:price];
        
        [self.tableView reloadData];
        
    }
    //反全选模块
    else if (type == selectToolTypeAllNo)
    {
        if (self.viewModel.modelsProxy.count==0){
            [self showBarMessage:@"亲,你没有商品!!"];
            [button setEnabled:NO];
            [button performBlock:^{
                [button setEnabled:YES];
            } afterDelay:1.25f];
            return ;
        }
        
        for (shopModel *model in self.viewModel.modelsProxy) {
            model.isSel = NO;
        }
        [self.selModels removeAllObjects];
        
        NSString *price = [NSString stringWithFormat:@"%.2f",[self onMuchPrice]];
        NSLog(@"%@",price);
        [self.toolBar onUpdatePrice:price];
        
        [self.tableView reloadData];
        
    }
    //结账模块
    else
    {
        NSLog(@"asdfghjkl");
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
